package homework0706;

import java.awt.event.MouseWheelListener;
import java.util.Random;
import java.util.Scanner;

public class niu {
    static Scanner scan = new Scanner(System.in);
    static int[] randomNumber = new int[4];

    static int[] userNumber = {-999999999, -999999999, -999999999, -999999999};

    public static void main(String[] args) {
        for (int i = 0; i < randomNumber.length; i++) {
            randomNumber[i] = (int) (Math.random() * 10);
            for (int j = 0; j < i; j++) {
                if (randomNumber[i] == randomNumber[j]) {
                    i--;
                    break;
                }
            }
        }
        System.out.print("生成的随机数为：");
        for (int item : randomNumber) {
            System.out.print(item + " ");
        }
        int i = 5;
        while (true) {
            System.out.println("你还有" + i + "次机会猜数字");
            try {
                System.out.println("请分别输入4个数");
                for (int j = 0; j < userNumber.length; j++) {
                    while (true){
                        int a = scan.nextInt();
                        if (a >= 0 && a <= 9) {
                            userNumber[j] = a;
                            break;
                        }else {
                            System.out.println("请输入0-9中的一个数字");
                        }
                    }
                }
                i--;
            } catch (Exception e) {
                System.out.println("请输入数字！！！");
                scan.next();
            }
            //判断输入的对不对
            int a = 0;  //统计有几个数字和位置是一样的
            int b = 0;  //统计有几个数字一样是一样的
            for (int j = 0; j < 4; j++) {
                if (randomNumber[j] == userNumber[j]) {
                    a++;
                }
                for (int k = 0; k < 4; k++) {
                    if (randomNumber[j] == userNumber[k]) {
                        b++;
                        break;
                    }
                }
            }
            int c = b-a; // 统计有几个数字一样但是位置不同
            System.out.println(a + "A" + c + "B");
            if (a == 4) { //如果全猜对的话就跳出循环
                break;
            }
            if (i <= 0) {//机会用完跳出循环
                break;
            }
        }
    }


}
